package edu.sctu.jsoup.service;


import edu.sctu.jsoup.entity.ClubEntity;
import edu.sctu.jsoup.repo.ClubRepo;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;

@Service
public class ClubService {

    @Autowired
    private ClubRepo clubRepo;


    public void run() throws IOException {
        //汽车之家
        Document document = Jsoup.connect("http://club.autohome.com." +
                "cn/#pvareaid=103419").get();

        Element element = document.getElementsByClass("forum-brand-box").first();

        Elements alla = element.getElementsByTag("a");

        ClubEntity clubEntity;
        for (int j = 0; j < alla.size(); j++) {

            clubEntity = new ClubEntity();
            Element everya = alla.get(j);

            String text = everya.text();
            String url = everya.attr("href");

            clubEntity.setTitle(text);
            clubEntity.setUrl("http://club.autohome.com.cn"+url);

           clubRepo.save(clubEntity);

        }
        int maxId = (int) clubRepo.count();
        System.out.println("表中最大的ID是："+maxId);

    }


}



